package com.g13.virtualpethospital.config;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;

/**
 * @Title: MongoDBConfig
 * @Author yyj1205
 * @Package com.g13.virtualpethospital.config
 * @Date 31/3/2024 21:14
 * @description: Mongo Config
 */
@Configuration
public class MongoDBConfig {
    @Value("${spring.datasource.mongodb.host}")
    String host;
    @Value("${spring.datasource.mongodb.port}")
    String port;
    @Value("${spring.datasource.mongodb.database}")
    String dbName;
    @Value("${spring.datasource.mongodb.username}")
    String userName;
    @Value("${spring.datasource.mongodb.password}")
    String password;

    @Bean
    public MongoClient mongoClient(){
        return MongoClients.create("mongodb://" + userName + ":" + password + "@" + host + ":" + port + "/" + dbName);
    }
    @Bean
    public MongoTemplate mongoTemplate(){
        return new MongoTemplate(mongoClient(), dbName);
    }
}
